package com.example.a16478082;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    private EditText et_r_un,et_r_psd;
    private Button btn_r_signIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        et_r_un.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b){
                    String unameRegex="^\\w+$";
                    String username=et_r_un.getText().toString();
                    if(username.equals("")){
                        Toast.makeText(getApplicationContext(),"用户名不能为空!",Toast.LENGTH_SHORT).show();
                    }else if(username.length()>8){
                        Toast.makeText(getApplicationContext(),"用户名太长",Toast.LENGTH_SHORT).show();
                    }else if(username.matches(unameRegex)){
                        Toast.makeText(getApplicationContext(),username+"：用户名有效",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "用户名含非法字符", Toast.LENGTH_SHORT).show();
                    }


                }
            }
        });

        et_r_psd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(!b){

                    String password=et_r_psd.getText().toString();
                    if (password.equals("")){
                        Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show();
                    }else if(password.length()<6||password.length()>12){
                        Toast.makeText(getApplicationContext(),"密码长度必须在6-12位之间",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getApplicationContext(),"密码有效",Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

        btn_r_signIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String unameRegex="^\\w+$";
                String username=et_r_un.getText().toString();
                String password=et_r_psd.getText().toString();
                Intent intent=new Intent();
                intent.setClass(RegisterActivity.this,LoginActivity.class);
                if(username.length()>=0&&username.length()<=8&&username.matches(unameRegex)&&password.length()>=6&&password.length()<=12){
                    RegisterActivity.this.startActivity(intent);
                }else {
                    Toast.makeText(getApplicationContext(),"用户名或密码不合法",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void initView() {
        et_r_un=findViewById(R.id.et_r_un);
        et_r_psd=findViewById(R.id.et_r_psd);
        btn_r_signIn=findViewById(R.id.btn_r_signIn);
    }
}
